/**
 * @(#)UserConverterImpl.java Jan 8, 2010
 *   
 * Copyright  (c) 2009-2010 Wilmar IT Service,  Inc.   
 * All  rights  reserved.   
 * TODO
 */

package com.wilmar.process.core.services.impl;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.jbpm.api.identity.Group;
import org.jbpm.api.identity.User;
import org.jbpm.pvm.internal.identity.spi.IdentitySession;

import com.wilmar.process.core.ProcessSystemDeclareKeys;
import com.wilmar.process.core.services.spi.IOrganizationService;
import com.wilmar.process.core.services.spi.IRoleService;
import com.wilmar.process.core.services.spi.IUserService;
import com.wilmar.process.entity.Organization;
import com.wilmar.process.entity.Role;
import com.wilmar.process.enumerate.EnumProcessBeanIdSet;
import com.wilmar.process.exception.DirtyDataException;
import com.wilmar.process.exception.UnSupportedMethodException;
import com.wilmar.process.system.ProcessSystem;

/**
 * @author MaKuBEX Email:makubexnet@gmail.com
 * @version 1.0.0
 */
public class EntityServiceImpl implements IdentitySession {

	private IUserService userService;
	private IRoleService roleService;
	private IOrganizationService organizationService;

	public EntityServiceImpl() {
		if (this.userService == null)
			this.userService = (IUserService) ProcessSystem
					.getBean(EnumProcessBeanIdSet.PROCESS_SYSTEM_USER_SERVICE);
		if (this.roleService == null)
			this.roleService = (IRoleService) ProcessSystem
					.getBean(EnumProcessBeanIdSet.PROCESS_SYSTEM_ROLE_SERVICE);
		if (this.organizationService == null)
			this.organizationService = (IOrganizationService) ProcessSystem
					.getBean(EnumProcessBeanIdSet.PROCESS_SYSTEM_ORGANIZATION_SERVICE);
	}

	/**
	 * @see org.jbpm.pvm.internal.identity.spi.IdentitySession#createGroup(java.lang.String,
	 *      java.lang.String, java.lang.String)
	 */
	public String createGroup(String arg0, String arg1, String arg2) {
		throw new UnSupportedMethodException("The class: "
				+ this.getClass().getName()
				+ " could not create a group!It's unsupported!");
	}

	/**
	 * @see org.jbpm.pvm.internal.identity.spi.IdentitySession#createMembership(java.lang.String,
	 *      java.lang.String, java.lang.String)
	 */
	public void createMembership(String arg0, String arg1, String arg2) {
		throw new UnSupportedMethodException("The class: "
				+ this.getClass().getName()
				+ " could not create a membership!It's unsupported!");
	}

	/**
	 * @see org.jbpm.pvm.internal.identity.spi.IdentitySession#createUser(java.lang
	 *      .String, java.lang.String, java.lang.String, java.lang.String)
	 */
	public String createUser(String arg0, String arg1, String arg2, String arg3) {
		throw new UnSupportedMethodException("The class: "
				+ this.getClass().getName()
				+ " could not create a user!It's unsupported!");
	}

	/**
	 * @see org.jbpm.pvm.internal.identity.spi.IdentitySession#deleteGroup(java.lang
	 *      .String)
	 */
	public void deleteGroup(String arg0) {
		throw new UnSupportedMethodException("The class: "
				+ this.getClass().getName()
				+ " could not delete a group!It's unsupported!");
	}

	/**
	 * @see org.jbpm.pvm.internal.identity.spi.IdentitySession#deleteMembership(java
	 *      .lang.String, java.lang.String, java.lang.String)
	 */
	public void deleteMembership(String arg0, String arg1, String arg2) {
		throw new UnSupportedMethodException("The class: "
				+ this.getClass().getName()
				+ " could not delete a membership!It's unsupported!");
	}

	/**
	 * @see org.jbpm.pvm.internal.identity.spi.IdentitySession#deleteUser(java.lang
	 *      .String)
	 */
	public void deleteUser(String arg0) {
		throw new UnSupportedMethodException("The class: "
				+ this.getClass().getName()
				+ " could not delete a user!It's unsupported!");
	}

	/**
	 * @see org.jbpm.pvm.internal.identity.spi.IdentitySession#findGroupById(java
	 *      .lang.String)
	 */
	public Group findGroupById(String arg0) {
		try {
			com.wilmar.process.entity.User tu = new com.wilmar.process.entity.User(
					arg0);
			tu = this.userService.getDetailUserByUserno(tu.getUserno(), tu
					.getOuterSystem());
			return tu;
		} catch (DirtyDataException e) {
			try {
				Role role = new Role(arg0);
				role = this.roleService.getDetailRoleByRoleCode(role
						.getRoleCode(), role.getOuterSystem());
				return role;
			} catch (DirtyDataException e1) {
				try {
					Organization org = new Organization(arg0);
					org = this.organizationService.getOrganizationByCode(org
							.getOrganizationCode(), org.getOuterSystem());
					return org;
				} catch (DirtyDataException e2) {
					e2.printStackTrace();
				}
			}
		}
		return null;
	}

	/**
	 * @see org.jbpm.pvm.internal.identity.spi.IdentitySession#findGroupsByUser(java
	 *      .lang.String)
	 */
	public List<Group> findGroupsByUser(String arg0) {
		com.wilmar.process.entity.User tu = null;
		try {
			tu = new com.wilmar.process.entity.User(arg0);
			tu = this.userService.getDetailUserByUserno(tu.getUserno(), tu
					.getOuterSystem());
		} catch (DirtyDataException e) {
			e.printStackTrace();
		}
		Set<Organization> orgSet = this.organizationService
				.getListAllOrganizationsByUser(tu);
		Set<Role> roleSet = this.roleService.getListRolesByUser(tu);
		Set<Group> allGroupSet = new HashSet<Group>();
		allGroupSet.addAll(roleSet);
		allGroupSet.addAll(orgSet);
		List<Group> userGroups = new ArrayList<Group>(allGroupSet);
		return userGroups;
	}

	/**
	 * @see org.jbpm.pvm.internal.identity.spi.IdentitySession#
	 *      findGroupsByUserAndGroupType (java.lang.String , java.lang. String)
	 */
	public List<Group> findGroupsByUserAndGroupType(String arg0, String arg1) {
		com.wilmar.process.entity.User tu = null;
		try {
			tu = new com.wilmar.process.entity.User(arg0);
			tu = this.userService.getDetailUserByUserno(tu.getUserno(), tu
					.getOuterSystem());
		} catch (DirtyDataException e) {
			e.printStackTrace();
		}
		Set<Group> groupSet = new HashSet<Group>();
		if (arg1
				.equalsIgnoreCase(ProcessSystemDeclareKeys.PROCESS_SYSTEM_PROCESS_ASSIGN_MARK_ORGANIZATION)) {
			Set<Organization> orgSet = this.organizationService
					.getListAllOrganizationsByUser(tu);
			groupSet.addAll(orgSet);
		} else if (arg1
				.equalsIgnoreCase(ProcessSystemDeclareKeys.PROCESS_SYSTEM_PROCESS_ASSIGN_MARK_ROLE)) {
			Set<Role> roleSet = this.roleService.getListRolesByUser(tu);
			groupSet.addAll(roleSet);
		} else {
			tu = this.userService.getDetailUserByUserno(tu.getUserno(), tu
					.getOuterSystem());
			groupSet.add(tu);
		}
		return new ArrayList<Group>(groupSet);
	}

	/**
	 * @see org.jbpm.pvm.internal.identity.spi.IdentitySession#findUserById(java.
	 *      lang.String)
	 */
	public User findUserById(String arg0) {
		User user = null;
		try {
			if (this.userService == null)
				this.userService = (IUserService) ProcessSystem.webAppContext
						.getBean("process_userService");
			com.wilmar.process.entity.User tmp = new com.wilmar.process.entity.User(
					arg0);
			user = this.userService.getDetailUserByUserno(tmp.getUserno(), tmp
					.getOuterSystem());
		} catch (DirtyDataException e) {
			e.printStackTrace();
		}
		return user;
	}

	/**
	 * @see org.jbpm.pvm.internal.identity.spi.IdentitySession#findUsers()
	 */
	public List<User> findUsers() {
		throw new UnSupportedMethodException("The class: "
				+ this.getClass().getName()
				+ " could not find all users!It's unsupported!");
	}

	/**
	 * @see org.jbpm.pvm.internal.identity.spi.IdentitySession#findUsersByGroup(java
	 *      .lang.String)
	 */
	public List<User> findUsersByGroup(String arg0) {
		Set<com.wilmar.process.entity.User> userSet = new HashSet<com.wilmar.process.entity.User>();
		try {
			Role role = new Role(arg0);
			userSet = this.userService.getListUsersByRole(role, role
					.getOuterSystem());
		} catch (DirtyDataException e1) {
			try {
				Organization org = new Organization(arg0);
				userSet = this.userService.getListUsersByOrganization(org, org
						.getOuterSystem());
			} catch (DirtyDataException e2) {
				e2.printStackTrace();
			}
		}
		return new ArrayList<User>(userSet);
	}

	/**
	 * @see org.jbpm.pvm.internal.identity.spi.IdentitySession#findUsersById(java
	 *      .lang.String[])
	 */
	public List<User> findUsersById(String... arg0) {
		List<User> userList = new ArrayList<User>();
		for (String userid : arg0) {
			com.wilmar.process.entity.User tmp = null;
			try {
				tmp = new com.wilmar.process.entity.User(userid);
			} catch (DirtyDataException e) {
				e.printStackTrace();
			}
			tmp = this.userService.getDetailUserByUserno(tmp.getUserno(), tmp
					.getOuterSystem());
			userList.add(tmp);
		}
		return userList;
	}

	/**
	 * @return the userService
	 */
	public IUserService getUserService() {
		return userService;
	}

	/**
	 * @param userService
	 *            the userService to set
	 */
	public void setUserService(IUserService userService) {
		this.userService = userService;
	}

	/**
	 * @return the roleService
	 */
	public IRoleService getRoleService() {
		return roleService;
	}

	/**
	 * @param roleService
	 *            the roleService to set
	 */
	public void setRoleService(IRoleService roleService) {
		this.roleService = roleService;
	}

	/**
	 * @return the organizationService
	 */
	public IOrganizationService getOrganizationService() {
		return organizationService;
	}

	/**
	 * @param organizationService
	 *            the organizationService to set
	 */
	public void setOrganizationService(IOrganizationService organizationService) {
		this.organizationService = organizationService;
	}

}
